package org.noote.libs.graphicbuffer;

import android.graphics.Rect;

public class BufferConvertion {
	// Method from Ketai project! Not mine! See below...
	public static void decodeYUV420SP(int[] argb, byte[] yuv420sp, int width, int height) {

		final int frameSize = width * height;

		for (int j = 0, yp = 0; j < height; j++) {
			int uvp = frameSize + (j >> 1) * width, u = 0, v = 0;
			for (int i = 0; i < width; i++, yp++) {
				int y = (0xff & (yuv420sp[yp])) - 16;
				if (y < 0)
					y = 0;
				if ((i & 1) == 0) {
					v = (0xff & yuv420sp[uvp++]) - 128;
					u = (0xff & yuv420sp[uvp++]) - 128;
				}

				int y1192 = 1192 * y;
				int r = (y1192 + 1634 * v);
				int g = (y1192 - 833 * v - 400 * u);
				int b = (y1192 + 2066 * u);

				if (r < 0)
					r = 0;
				else if (r > 262143)
					r = 262143;
				if (g < 0)
					g = 0;
				else if (g > 262143)
					g = 262143;
				if (b < 0)
					b = 0;
				else if (b > 262143)
					b = 262143;

				argb[yp] = 0xff000000 | ((r << 6) & 0xff0000)
						| ((g >> 2) & 0xff00) | ((b >> 10) & 0xff);
			}
		}
	}
	

	public static int[] decodeYUV420SP(byte[] yuv420sp, int width, int height, Rect rect)
	{
		final int frameSize = width * height;
		int ow = rect.right - rect.left;
		int oh = rect.bottom - rect.top;
		int rgb[] = new int[ow * oh];
		for (int j = 0, yp = 0, ic = 0; j < height; j++)
		{
			if (j <= rect.top)
			{
				yp += width;
				continue;
			}
			if (j > rect.bottom) break;
			
			int uvp = frameSize + (j >> 1) * width, u = 0, v = 0;
			for (int i = 0; i < width; i++, yp++)
			{
				int y = (0xff & ((int) yuv420sp[yp])) - 16;
				if (y < 0)	y = 0;
				if ((i & 1) == 0)
				{
					v = (0xff & yuv420sp[uvp++]) - 128;
					u = (0xff & yuv420sp[uvp++]) - 128;
				}
				if (i <= rect.left || i > rect.right) continue;
				
				int y1192 = 1192 * y;
				int r = (y1192 + 1634 * v);
				int g = (y1192 - 833 * v - 400 * u);
				int b = (y1192 + 2066 * u);

				if (r < 0)				r = 0;
				else if (r > 262143)	r = 262143;
				if (g < 0)				g = 0;
				else if (g > 262143)	g = 262143;
				if (b < 0)				b = 0;
				else if (b > 262143)	b = 262143;

				int rx = 0xff000000 | ((r << 6) & 0xff0000)	| ((g >> 2) & 0xff00) | ((b >> 10) & 0xff);
				rgb[ic] = rx;
				ic++;
			}
		}
		return rgb;
	}
	
	// decode Y, U, and V values on the YUV 420 buffer described as
	//YCbCr_422_SP by Android
	// 		David Manpearl 081201
	public static void decodeYUV422SP(int[] argb, byte[] fg, int width, int height)
	{
	        final int sz = width * height;
	        int i, j;
	        int Y, Cr = 0, Cb = 0;
	        for(j = 0; j < height; j++) {
	                int pixPtr = j * width;
	                final int jDiv2 = j >> 1;
	                for(i = 0; i < width; i++) {
	                        Y = fg[pixPtr]; if(Y < 0) Y += 255;
	                        if((i & 0x1) != 1) {
	                                final int cOff = sz + jDiv2 * width + (i >> 1) * 2;
	                                Cb = fg[cOff];
	                                if(Cb < 0) Cb += 127; else Cb -= 128;
	                                Cr = fg[cOff + 1];
	                                if(Cr < 0) Cr += 127; else Cr -= 128;
	                        }
	                        int R = Y + Cr + (Cr >> 2) + (Cr >> 3) + (Cr >> 5);
	                        if(R < 0) R = 0; else if(R > 255) R = 255;
	                        int G = Y - (Cb >> 2) + (Cb >> 4) + (Cb >> 5) - (Cr >> 1) + (Cr >>
	3) + (Cr >> 4) + (Cr >> 5);
	                        if(G < 0) G = 0; else if(G > 255) G = 255;
	                        int B = Y + Cb + (Cb >> 1) + (Cb >> 2) + (Cb >> 6);
	                        if(B < 0) B = 0; else if(B > 255) B = 255;
	                        argb[pixPtr++] = 0xff000000 + (B << 16) + (G << 8) + R;
	                }
	        }
	}
	static public void encodeYUV420SP(byte[] yuv420sp, int[] argb,
	        int width, int height) {
	    final int frameSize = width * height;

	    int[] U, V;
	    U = new int[frameSize];
	    V = new int[frameSize];

	    int r, g, b, y, u, v;
	    for (int j = 0; j < height; j++) {
	        int index = width * j;
	        for (int i = 0; i < width; i++) {
	            r = (argb[index] & 0xff0000) >> 16;
	            g = (argb[index] & 0xff00) >> 8;
	            b = (argb[index] & 0xff);

	            // rgb to yuv
	            y = (66 * r + 129 * g + 25 * b + 128) >> 8 + 16;
	            u = (-38 * r - 74 * g + 112 * b + 128) >> 8 + 128;
	            v = (112 * r - 94 * g - 18 * b + 128) >> 8 + 128;

	            // clip y
	            yuv420sp[index++] = (byte) ((y < 0) ? 0 : ((y > 255) ? 255 : y));
	            U[index] = u;
	            V[index++] = v;
	        }
	    }
	}
	
	public static void decodeRGB565(int[] argb, byte[] rgb565, int width, int height) {

		for (int j = 0, yp = 0; j < height; j++) {
			for (int i = 0; i < width; i++, yp+=2) {
				
				int r = 0xF8 & rgb565[yp];
				int g = (0x07 & rgb565[yp])<<3 | (0xE0 & rgb565[yp+1]);
				int b = 0xF1 & rgb565[yp+1];
				
				r*=8;
				g*=4;
				b*=8;

				argb[j*width+i] = 0xff000000 | ((r << 6) & 0xff0000)
					| ((g >> 2) & 0xff00) | ((b >> 10) & 0xff);
			}
		}
	}

	public static void decodeRGB888(int[] argb, byte[] rgb888, int width, int height) {

		for (int j = 0, yp = 0; j < height; j++) {
			for (int i = 0; i < width; i++, yp+=3) {
				
				int r = rgb888[yp];
				int g = rgb888[yp+1];
				int b = rgb888[yp+2];
				
				argb[j*width+i] = 0xff000000 | ((r << 6) & 0xff0000)
					| ((g >> 2) & 0xff00) | ((b >> 10) & 0xff);
			}
		}
	}
}
